package com.example.contestsystem.Controller;

import com.example.contestsystem.Entity.Contest;
import com.example.contestsystem.Entity.Student;
import com.example.contestsystem.Entity.Team;
import com.example.contestsystem.R.R;
import com.example.contestsystem.Service.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/stu")
public class StuController {
    final StuService stuService;
    final JoinTeamService joinTeamService;
    final ContestService contestService;
    final SignUpContestService signUpContestService;
    final TeamService teamService;

    public StuController(StuService stuService, JoinTeamService joinTeamService,
                         ContestService contestService, SignUpContestService signUpContestService,
                         TeamService teamService) {
        this.stuService = stuService;
        this.joinTeamService = joinTeamService;
        this.contestService = contestService;
        this.signUpContestService = signUpContestService;
        this.teamService = teamService;
    }

    @GetMapping("/login")
    public String stu() {
        return "stu/login";
    }


    @PostMapping("/register")
    public String register(@ModelAttribute Student stu, HttpServletRequest httpServletRequest) {
        if (this.stuService.register(stu) != null) {
            httpServletRequest.getSession().setAttribute("loginStu", stu);
            return "redirect:/contest";
        } else return "stu/login";
    }


    @PostMapping("/login")
    @ResponseBody
    public R login(@ModelAttribute Student student, HttpServletRequest httpServletRequest) {
        if (this.stuService.login(student)) {
            // TODO: 2023/12/24 此代码可能太耗性能 
            student.setStuName(this.stuService.findByStuId(student.getStuAccount()).getStuName());
            httpServletRequest.getSession().setAttribute("loginStu", student);
            httpServletRequest.getSession().setAttribute("loginTeacher", null);
            return R.ok(200, "登录成功");
        }
        return R.error();
    }


    /**
     *
     */
    @GetMapping("/profile")
    public String profile(HttpServletRequest httpServletRequest, Model model) {
        Student loginStu = (Student) httpServletRequest.getSession().getAttribute("loginStu");
        if (loginStu != null) {
            return "stu/information_b";
        }
        return "stu/login";
    }

    @PostMapping("/modify")
    public String modify(@ModelAttribute Student student, HttpServletRequest httpServletRequest){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if(student.getStuName() != null && !student.getStuName().isEmpty()){
            loginStu.setStuName(student.getStuName());
        }
        if(student.getStuPassword() != null && !student.getStuPassword().isEmpty()){
            loginStu.setStuPassword(student.getStuPassword());
        }
        this.stuService.modify(loginStu);
        return "redirect:/stu/profile";
    }

    @GetMapping("/get_stu")
    @ResponseBody
    public Object getStu(HttpServletRequest httpServletRequest){
        Student loginStu = (Student) httpServletRequest.getSession().getAttribute("loginStu");
        Student student = stuService.findByStuId(loginStu.getStuAccount());
        if(loginStu != null ){
            return R.ok("成功",student,200);
        }
        return R.error(-1,"您未登录");
    }

    @GetMapping("/verifyStu")
    @ResponseBody
    public String verifyStu(@Param("stuAccount") int stuAccount){
        if(this.stuService.verifyStu(stuAccount))
            return "true";
        return "false";
    }

    @GetMapping("/participatedContest")
    public String contestList(HttpServletRequest httpServletRequest, Model model){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if(loginStu!=null) {
            Page<Contest> contests = this.contestService.findByStuAccount(loginStu.getStuAccount(), 0, 20);
            int prePage = contests.previousOrFirstPageable().getPageNumber();
            int nextPage = contests.nextOrLastPageable().getPageNumber();
            model.addAttribute("contests", contests);
            model.addAttribute("prePage", prePage);
            model.addAttribute("nextPage", nextPage);
            return "stu/contest_list";
        }
        return "stu/login";
    }

    @GetMapping("/joinedTeam")
    public String teamList(HttpServletRequest httpServletRequest, Model model){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if(loginStu != null){
            Page<Team> teams = this.teamService.findByStuAccount(loginStu.getStuAccount(), 0, 20);
            int prePage = teams.previousOrFirstPageable().getPageNumber();
            int nextPage = teams.nextOrLastPageable().getPageNumber();
            model.addAttribute("teams", teams);
            model.addAttribute("prePage", prePage);
            model.addAttribute("nextPage", nextPage);
            return "stu/team_list";
        }
        return "stu/login";
    }

    @GetMapping("/register")
    public String register(){
        return "stu/register";
    }
}
